package com.lcq.study.leetcode;

/**
 * @ClassName T20220425
 * @Description
 * @Author lichuanqi
 * @Date 2022/4/25 14:21
 * @Version 1.0
 **/
public class T20220425 {
    public static void main(String[] args) {
        T20220425 test = new T20220425();
        int num1[] = {4,9,5};
        int num2[] = {9,4,9,8,4};
        int[] intersect = test.intersect(num1, num2);
        for (int i = 0; i < intersect.length; i++) {
            System.out.printf("%d ", intersect[i]);
        }
        System.out.println("");
    }

    public int[] intersect(int[] nums1, int[] nums2) {
        int resLen = nums1.length < nums2.length ? nums1.length : nums2.length;
        int visitLen = nums1.length > nums2.length ? nums1.length : nums2.length;
        int[] resNums = new int[resLen];
        boolean[] visit = new boolean[visitLen];
        int k = 0;
        for (int i = 0; i < nums1.length; i++) {
            for (int j = 0; j < nums2.length; j++) {
                if (nums1[i] == nums2[j] && visit[j] == false){
                    resNums[k] = nums1[i];
                    k++;
                    if (k == resLen){
                        return resNums;
                    }
                    visit[j] = true;
                    break;
                }
            }
        }

        int[] resNums1 = new int[k];
        for (int i = 0; i < k; i++) {
            resNums1[i] = resNums[i];
        }
        return resNums1;
    }
}
